package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Student;
import com.example.service.StudentService;
import com.example.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

//    List<Users> users=userService.findAll(null);
//        if (map.size()==0 || map.get("currentPage").isEmpty()){
//        map.put("currentPage","1");
//    }
//    PageUtil currentPage=new PageUtil(Integer.valueOf(map.get("currentPage")),2,users);
//        model.addAttribute("users",currentPage.getList());
//        model.addAttribute("currentPage",currentPage);
//        return "users";
//    @GetMapping("page")
//    public String findPage(@RequestParam Map<String,String> map, Model model,String names){
//
//        QueryWrapper<Student> wrapper = new QueryWrapper<>();
//
//        if (names==null){
//            names="";
//        }
//        wrapper.like("name",names);
//
//        List<Student> students=studentService.findAll(wrapper);
//        model.addAttribute("name",names);
//       if (map.size()==0 || map.get("currentPage").isEmpty()){
//           map.put("currentPage","1");
//       }
//        PageUtil currentPage=new PageUtil(Integer.valueOf(map.get("currentPage")),2,students);
//        model.addAttribute("students",currentPage.getList());
//        model.addAttribute("currentPage",currentPage);
//        return "list";
//
//}


    @GetMapping("page")
    public String findAll(Model model,Page page,String names){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if (names==null){
            names="";
        }

        queryWrapper.like("name",names);
        Page<Student> page1=new Page<>(page.getCurrent(),2);
        Page<Student> pageInfo=studentService.findSll(page1,queryWrapper);
        model.addAttribute("students",pageInfo);
        return "list";
    }
//    @RequestMapping("selectName")
//    public String selectName(@RequestParam("name") String name){
//        System.out.println("执行模糊查询");
//          studentService.selectName(name);
//        System.out.println("重新查询");
//        return "redirect:/page";
//    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        Student student = new Student();
        model.addAttribute("students",student);
        return "add";
    }

    @PostMapping("add")
    public String add(Student student){
        studentService.add(student);
        return "redirect:/page";
    }


    @PostMapping("update")
    public String update(Student student){
        studentService.update(student);
        return "redirect:/page";
    }

    @RequestMapping("findById")
    public String findById(String id,Model model){
        Student student=studentService.findById(id);
        model.addAttribute("students",student);
        return "update";
    }

    @RequestMapping("del")
    public String del(String id){
        studentService.del(id);
        return "redirect:/page";
    }
}
